<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>🌟 智慧童话世界 - AI可选分支剧情童话书</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        
        body {
            font-family: 'Microsoft YaHei', Arial, sans-serif;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            min-height: 100vh;
            color: #333;
        }
        
        .container {
            max-width: 1200px;
            margin: 0 auto;
            padding: 20px;
        }
        
        .header {
            text-align: center;
            margin-bottom: 30px;
            background: rgba(255, 255, 255, 0.95);
            padding: 30px;
            border-radius: 20px;
            box-shadow: 0 10px 30px rgba(0, 0, 0, 0.2);
        }
        
        .header h1 {
            font-size: 2.5em;
            color: #4a5568;
            margin-bottom: 10px;
            text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.1);
        }
        
        .header p {
            font-size: 1.2em;
            color: #666;
            margin-bottom: 15px;
        }
        
        .tech-badges {
            display: flex;
            justify-content: center;
            gap: 15px;
            flex-wrap: wrap;
            margin-top: 20px;
        }
        
        .badge {
            background: linear-gradient(45deg, #ff6b6b, #ee5a24);
            color: white;
            padding: 8px 16px;
            border-radius: 20px;
            font-size: 0.9em;
            font-weight: bold;
            box-shadow: 0 4px 15px rgba(0, 0, 0, 0.2);
        }
        
        .main-content {
            display: grid;
            grid-template-columns: 1fr 1fr;
            gap: 30px;
            margin-bottom: 30px;
        }
        
        .input-section, .story-section {
            background: rgba(255, 255, 255, 0.95);
            padding: 30px;
            border-radius: 20px;
            box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
        }
        
        .section-title {
            font-size: 1.5em;
            color: #4a5568;
            margin-bottom: 20px;
            text-align: center;
            border-bottom: 3px solid #667eea;
            padding-bottom: 10px;
        }
        
        .form-group {
            margin-bottom: 20px;
        }
        
        .form-group label {
            display: block;
            margin-bottom: 8px;
            font-weight: bold;
            color: #4a5568;
        }
        
        .form-group input, .form-group select, .form-group textarea {
            width: 100%;
            padding: 12px;
            border: 2px solid #e2e8f0;
            border-radius: 10px;
            font-size: 1em;
            transition: all 0.3s ease;
        }
        
        .form-group input:focus, .form-group select:focus, .form-group textarea:focus {
            outline: none;
            border-color: #667eea;
            box-shadow: 0 0 10px rgba(102, 126, 234, 0.3);
        }
        
        .theme-grid {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(120px, 1fr));
            gap: 10px;
            margin-top: 10px;
        }
        
        .theme-option {
            padding: 15px 10px;
            border: 2px solid #e2e8f0;
            border-radius: 10px;
            text-align: center;
            cursor: pointer;
            transition: all 0.3s ease;
            background: white;
        }
        
        .theme-option:hover {
            border-color: #667eea;
            background: #f7fafc;
            transform: translateY(-2px);
        }
        
        .theme-option.selected {
            border-color: #667eea;
            background: #667eea;
            color: white;
        }
        
        .generate-btn {
            width: 100%;
            padding: 15px;
            background: linear-gradient(45deg, #667eea, #764ba2);
            color: white;
            border: none;
            border-radius: 10px;
            font-size: 1.2em;
            font-weight: bold;
            cursor: pointer;
            transition: all 0.3s ease;
            margin-top: 20px;
        }
        
        .generate-btn:hover {
            transform: translateY(-2px);
            box-shadow: 0 10px 25px rgba(102, 126, 234, 0.4);
        }
        
        .generate-btn:disabled {
            opacity: 0.6;
            cursor: not-allowed;
            transform: none;
        }
        
        .story-display {
            min-height: 300px;
            background: #f8f9fa;
            border-radius: 10px;
            padding: 20px;
            margin-bottom: 20px;
            border-left: 5px solid #667eea;
            line-height: 1.8;
        }
        
        .story-content {
            font-size: 1.1em;
            color: #4a5568;
            white-space: pre-line;
        }
        
        .choices-section {
            margin-top: 20px;
        }
        
        .choice-btn {
            display: block;
            width: 100%;
            padding: 15px;
            margin-bottom: 10px;
            background: white;
            border: 2px solid #667eea;
            border-radius: 10px;
            color: #667eea;
            font-size: 1em;
            cursor: pointer;
            transition: all 0.3s ease;
        }
        
        .choice-btn:hover {
            background: #667eea;
            color: white;
            transform: translateX(5px);
        }
        
        .loading {
            text-align: center;
            padding: 40px;
            color: #667eea;
            font-size: 1.2em;
        }
        
        .loading::after {
            content: '';
            display: inline-block;
            width: 20px;
            height: 20px;
            border: 3px solid #667eea;
            border-radius: 50%;
            border-top-color: transparent;
            animation: spin 1s linear infinite;
            margin-left: 10px;
        }
        
        @keyframes spin {
            to { transform: rotate(360deg); }
        }
        
        .features-section {
            background: rgba(255, 255, 255, 0.95);
            padding: 30px;
            border-radius: 20px;
            box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
            margin-top: 30px;
        }
        
        .features-grid {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
            gap: 20px;
            margin-top: 20px;
        }
        
        .feature-card {
            background: linear-gradient(135deg, #667eea, #764ba2);
            color: white;
            padding: 25px;
            border-radius: 15px;
            text-align: center;
            box-shadow: 0 8px 25px rgba(0, 0, 0, 0.2);
        }
        
        .feature-card h3 {
            font-size: 1.3em;
            margin-bottom: 15px;
        }
        
        .feature-card p {
            line-height: 1.6;
            opacity: 0.9;
        }
        
        .reset-btn {
            background: linear-gradient(45deg, #ff6b6b, #ee5a24);
            color: white;
            border: none;
            padding: 10px 20px;
            border-radius: 8px;
            cursor: pointer;
            font-size: 0.9em;
            margin-top: 10px;
            transition: all 0.3s ease;
        }
        
        .reset-btn:hover {
            transform: translateY(-2px);
            box-shadow: 0 5px 15px rgba(255, 107, 107, 0.4);
        }
        
        @media (max-width: 768px) {
            .main-content {
                grid-template-columns: 1fr;
            }
            
            .header h1 {
                font-size: 2em;
            }
            
            .tech-badges {
                justify-content: center;
            }
        }
    </style>
</head>
<body>
    <div class="container">
        <!-- 头部 -->
        <div class="header">
            <h1>🌟 智慧童话世界</h1>
            <p>基于ERNIE-4.5大模型 + 智源研究院数据的AI可选分支剧情童话书</p>
            <div class="tech-badges">
                <span class="badge">🤖 ERNIE-4.5-0.3B-Paddle</span>
                <span class="badge">🧠 智源研究院数据</span>
                <span class="badge">🔗 MCP协议集成</span>
                <span class="badge">🎨 多模态交互</span>
            </div>
        </div>
        
        <!-- 主要内容 -->
        <div class="main-content">
            <!-- 输入区域 -->
            <div class="input-section">
                <h2 class="section-title">🎯 个性化设置</h2>
                
                <div class="form-group">
                    <label for="childName">👶 小朋友的姓名：</label>
                    <input type="text" id="childName" placeholder="请输入小朋友的姓名" value="小明">
                </div>
                
                <div class="form-group">
                    <label for="childAge">🎂 年龄段：</label>
                    <select id="childAge">
                        <option value="3-5岁">3-5岁 (简单故事)</option>
                        <option value="6-8岁" selected>6-8岁 (适中复杂度)</option>
                        <option value="9-12岁">9-12岁 (复杂情节)</option>
                    </select>
                </div>
                
                <div class="form-group">
                    <label>🎭 故事主题：</label>
                    <div class="theme-grid">
                        <div class="theme-option" data-theme="勇敢与友谊">
                            🦸<br>勇敢与友谊
                        </div>
                        <div class="theme-option selected" data-theme="神奇的森林">
                            🌲<br>神奇的森林
                        </div>
                        <div class="theme-option" data-theme="海底探险">
                            🌊<br>海底探险
                        </div>
                        <div class="theme-option" data-theme="太空旅行">
                            🚀<br>太空旅行
                        </div>
                        <div class="theme-option" data-theme="动物王国">
                            🐾<br>动物王国
                        </div>
                    </div>
                </div>
                
                <div class="form-group">
                    <label for="interests">💝 兴趣爱好：</label>
                    <textarea id="interests" rows="3" placeholder="请输入兴趣爱好，用逗号分隔，如：动物, 冒险, 科学">动物, 冒险, 友谊</textarea>
                </div>
                
                <button class="generate-btn" onclick="generateStory()">
                    🚀 开始生成专属童话
                </button>
            </div>
            
            <!-- 故事显示区域 -->
            <div class="story-section">
                <h2 class="section-title">📖 童话故事</h2>
                
                <div class="story-display">
                    <div class="story-content" id="storyContent">
                        欢迎来到智慧童话世界！🌟
                        
                        这里是一个神奇的地方，AI会为每个小朋友创造独一无二的童话故事。
                        
                        请在左侧设置小朋友的信息，然后点击"开始生成专属童话"按钮，让我们一起开始这段奇妙的旅程吧！
                        
                        ✨ 每个故事都融入了智源研究院的教育数据
                        🎯 根据年龄段智能调整内容难度
                        🌈 支持多种主题和个性化定制
                        🔀 可选择的分支剧情，每次都有新体验
                    </div>
                </div>
                
                <div class="choices-section" id="choicesSection" style="display: none;">
                    <h3>🔀 请选择接下来的剧情：</h3>
                    <div id="choiceButtons"></div>
                    <button class="reset-btn" onclick="resetStory()">🔄 重新开始新故事</button>
                </div>
            </div>
        </div>
        
        <!-- 功能特色 -->
        <div class="features-section">
            <h2 class="section-title">🌈 应用特色</h2>
            <div class="features-grid">
                <div class="feature-card">
                    <h3>🤖 AI智能生成</h3>
                    <p>基于ERNIE-4.5-0.3B-Paddle大模型，为每个孩子生成独特的个性化故事内容</p>
                </div>
                <div class="feature-card">
                    <h3>🧠 科研数据支撑</h3>
                    <p>融入智源研究院深度科研数据，确保内容符合儿童认知发展规律</p>
                </div>
                <div class="feature-card">
                    <h3>🎭 分支剧情系统</h3>
                    <p>可选择的故事发展路径，每个选择都会影响故事走向，体验无限可能</p>
                </div>
                <div class="feature-card">
                    <h3>📚 个性化学习</h3>
                    <p>根据年龄段和兴趣爱好智能调整内容，寓教于乐，培养多元能力</p>
                </div>
            </div>
        </div>
    </div>
    <script src="/static/enhanced_script.js"></script>
    <script>
        // 内联JavaScript代码
        let selectedTheme = '神奇的森林';
        let currentStoryData = null;

        // 页面加载完成后初始化
        document.addEventListener('DOMContentLoaded', function() {
            initializeThemeSelection();
        });

        // 初始化主题选择
        function initializeThemeSelection() {
            const themeOptions = document.querySelectorAll('.theme-option');
            
            themeOptions.forEach(option => {
                option.addEventListener('click', function() {
                    themeOptions.forEach(opt => opt.classList.remove('selected'));
                    this.classList.add('selected');
                    selectedTheme = this.getAttribute('data-theme');
                });
            });
        }

        // 生成故事
        async function generateStory() {
            const childName = document.getElementById('childName').value.trim();
            const childAge = document.getElementById('childAge').value;
            const interests = document.getElementById('interests').value.trim();
            
            if (!childName) {
                alert('请输入小朋友的姓名！');
                return;
            }
            
            showLoading();
            
            try {
                const response = await fetch('/api/generate_story', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json',
                    },
                    body: JSON.stringify({
                        name: childName,
                        age: childAge,
                        theme: selectedTheme,
                        interests: interests
                    })
                });
                
                const result = await response.json();
                
                if (result.success) {
                    currentStoryData = result;
                    displayStory(result.story);
                    displayChoices(result.choices);
                } else {
                    showError('故事生成失败：' + result.error);
                }
                
            } catch (error) {
                console.error('生成故事时出错:', error);
                showError('网络连接错误，请稍后重试');
            }
        }

        // 显示故事内容
        function displayStory(storyText) {
            const storyContent = document.getElementById('storyContent');
            storyContent.innerHTML = storyText;
        }

        // 显示选择按钮
        function displayChoices(choices) {
            const choicesSection = document.getElementById('choicesSection');
            const choiceButtons = document.getElementById('choiceButtons');
            
            choiceButtons.innerHTML = '';
            
            choices.forEach((choice, index) => {
                const button = document.createElement('button');
                button.className = 'choice-btn';
                button.textContent = `${index + 1}. ${choice}`;
                button.onclick = () => makeChoice(index);
                choiceButtons.appendChild(button);
            });
            
            choicesSection.style.display = 'block';
        }

        // 处理用户选择
        async function makeChoice(choiceIndex) {
            showLoading();
            
            try {
                const response = await fetch('/api/make_choice', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json',
                    },
                    body: JSON.stringify({
                        choice_index: choiceIndex
                    })
                });
                
                const result = await response.json();
                
                if (result.success) {
                    displayStory(result.story);
                    displayChoices(result.choices);
                } else {
                    showError('选择处理失败：' + result.error);
                }
                
            } catch (error) {
                console.error('处理选择时出错:', error);
                showError('网络连接错误，请稍后重试');
            }
        }

        // 重置故事
        async function resetStory() {
            if (confirm('确定要重新开始新的故事吗？')) {
                try {
                    await fetch('/api/reset_story', {
                        method: 'POST',
                        headers: {
                            'Content-Type': 'application/json',
                        }
                    });
                    
                    document.getElementById('storyContent').innerHTML = `
                        欢迎来到智慧童话世界！🌟
                        
                        这里是一个神奇的地方，AI会为每个小朋友创造独一无二的童话故事。
                        
                        请在左侧设置小朋友的信息，然后点击"开始生成专属童话"按钮，让我们一起开始这段奇妙的旅程吧！
                        
                        ✨ 每个故事都融入了智源研究院的教育数据
                        🎯 根据年龄段智能调整内容难度
                        🌈 支持多种主题和个性化定制
                        🔀 可选择的分支剧情，每次都有新体验
                    `;
                    
                    document.getElementById('choicesSection').style.display = 'none';
                    currentStoryData = null;
                    
                } catch (error) {
                    console.error('重置故事时出错:', error);
                    showError('重置失败，请刷新页面重试');
                }
            }
        }

        // 显示加载状态
        function showLoading() {
            const storyContent = document.getElementById('storyContent');
            storyContent.innerHTML = '<div class="loading">正在生成精彩的故事</div>';
            document.getElementById('choicesSection').style.display = 'none';
        }

        // 显示错误信息
        function showError(message) {
            const storyContent = document.getElementById('storyContent');
            storyContent.innerHTML = `
                <div style="color: #e53e3e; text-align: center; padding: 20px;">
                    ❌ ${message}
                    <br><br>
                    <button onclick="location.reload()" style="padding: 10px 20px; background: #667eea; color: white; border: none; border-radius: 5px; cursor: pointer;">
                        🔄 刷新页面
                    </button>
                </div>
            `;
        }
    </script>
</body>
</html>